package com.softeem.xyblogmaster.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.softeem.xyblogmaster.jwt.JwtUtil;
import com.softeem.xyblogmaster.domain.Users;
import com.softeem.xyblogmaster.service.AuthService;
import com.softeem.xyblogmaster.util.AjaxResult;
import com.softeem.xyblogmaster.vo.UserVO;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/auth")
public class AuthController {


    @Autowired
    private AuthService authService;


    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisTemplate redisTemplate;

//    @RequestMapping("/doLogin")
//    public AjaxResult doLogin(@RequestBody @Valid  Users users)
//    {
//        Users u = authService.doLogin(users);
//        String token = jwtUtil.generateToken(u.getId());
//        return AjaxResult.ok("登录成功",token);
//    }

    @RequestMapping("/doLogin")
    public AjaxResult doLogin(@RequestBody @Valid UserVO userVO)
    {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code = (String)valueOperations.get("captcha:" + userVO.getUuid());
        if(code==null){
            return AjaxResult.fail("验证码已过期,请重新输入",null);
        }
        MathGenerator mathGenerator=new MathGenerator(1);
        boolean verify = mathGenerator.verify(code, userVO.getResult());
        if(verify) {
            Users users=new Users();
            BeanUtil.copyProperties(userVO,users);
            Users u = authService.doLogin(users);
            String token = jwtUtil.generateToken(u.getId());
            return AjaxResult.ok("登录成功", token);
        }
        else{
            return AjaxResult.fail("验证码无效,请重新输入",null);
        }
    }

    @RequestMapping("/doRegister")
    public AjaxResult doRegister(@RequestBody @Valid Users users)
    {
        authService.doRegister(users);
        return AjaxResult.ok("注册成功",null);
    }

    //验证码功能
    @GetMapping("/getImage")
    public void buildValidateCode(HttpServletResponse response) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
//
        //      CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        captcha.write(response.getOutputStream());
    }

    @GetMapping("/getCaptcha2222")
    public AjaxResult getCaptcha(HttpServletResponse response) throws IOException
    {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);

        MathGenerator mathGenerator = new MathGenerator(1);
        captcha.setGenerator(mathGenerator);
        captcha.createCode();

        System.out.println(captcha.getCode());

        String code= captcha.getCode();
        String simpleUUID = IdUtil.simpleUUID();
        String savePath = "D://captcha//" + simpleUUID + ".png";
        captcha.write(savePath);
        Base64.Encoder encoder = Base64.getEncoder();
        String picture_code = encoder.encodeToString(FileUtil.readBytes(savePath));
        Map resultMap  = new HashMap();
        resultMap.put("img",picture_code);
        resultMap.put("uuid",simpleUUID);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("captcha:"+simpleUUID,code,10, TimeUnit.MINUTES);
        return AjaxResult.ok("获取验证码成功",resultMap);
    }

}

